package com.radon.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/**
 * 确认消息已到达交换机
 * 确认到达监听(生产者消息确认组件)
 */
@Configuration
public class RabbitSendConfirmHandler implements RabbitTemplate.ConfirmCallback {

    private static Logger logger = LoggerFactory.getLogger(RabbitSendConfirmHandler.class);
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        System.out.println("correlationData就是订单id:" + correlationData.getId());
        System.out.println("到达状态:" + ack);
        if (!ack) {
            /**
             * 此处应该搞个计数。超过3次就不发了，上定时任务,此处统计订单id和次数存到redis
             */
            Message message = correlationData.getReturnedMessage();
            MessageProperties messageProperties = correlationData.getReturnedMessage().getMessageProperties();
            System.out.println("消费发送失败异常处理");
            rabbitTemplate.convertAndSend(messageProperties.getReceivedExchange(), messageProperties.getReceivedRoutingKey(), message, correlationData);
        }
    }
}